package com.xyy.saas.payment.merchant.core.dto;

import com.xyy.saas.payment.common.enums.AmountTypeEnum;
import com.xyy.saas.payment.common.exception.PaymentException;
import com.xyy.saas.payment.cores.enums.FbankTradeCodeEnum;
import com.xyy.saas.payment.cores.enums.ResultCodeEnum;
import com.xyy.saas.payment.dao.model.SeparateOrder;
import com.xyy.saas.payment.util.DateUtil;
import lombok.Builder;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

@Data
@Builder
public class AccountTransferDto {
    public static final String TRANSFER_PAY = "9";
    public static final String TRANSFER_PAY_T1= "6";
    public static final String TRANSFER_REFUND = "7";
    public static final String TRANSFER_X = "X";

    /**
     * 转账类型
     * 6 直接支付T+1
     * 9 直接支付T+0
     * 7 支付到平台（会员——>平台）
     * X: 资方交易（普通会员A与资方子账户（普通或商户）互转）
     */
    private String transferType;
    /**
     * 业务线
     */
    private String businessIdType;
    /**
     * 支付平台商户标识（转出）
     */
    private String outAccountId;
    /**
     * 支付平台商户标识（转入）
     */
    private String inAccountId;
    /**
     * 金额
     */
    private Integer amount;
    /**
     * 手续费
     */
    private Integer fee;
    /**
     * 交易单号
     */
    private String transferNo;
    /**
     * 业务单号
     */
    private String businessOrderNo;
    private String bizNo;
    private String orderNo;
    /**
     * 备注
     */
    private String remark;

    private String extra;

    /**
     * 重写get方法
     */
    public Integer getFee() {
        return fee == null ? NumberUtils.INTEGER_ZERO : fee;
    }

    /**
     * 参数校验
     */
    public void checkParam() throws PaymentException {
        if(StringUtils.isBlank(transferType)) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR.getCode(), "转账类型不能为空");
        }

        if(StringUtils.isBlank(businessIdType)) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR.getCode(), "业务线不能为空");
        }

        if(StringUtils.isBlank(outAccountId)) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR.getCode(), "出金账户不能为空");
        }

        if(StringUtils.isBlank(inAccountId)) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR.getCode(), "入金账户不能为空");
        }

        if(amount <= NumberUtils.INTEGER_ZERO) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR.getCode(), "金额必须大于0");
        }

        if(StringUtils.isBlank(businessOrderNo)) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR.getCode(), "业务单号不能为空");
        }
    }

    private void fillSeparateOrderBizNo(SeparateOrder separateOrder) {
        String bizOrderNo = separateOrder.getBusinessOrderNo();
        // 平台补贴
        if (bizOrderNo.contains(AmountTypeEnum.PLATFORM_DISCOUNT_AMOUNT.name())) {
            // RZD2025011602447_PLATFORM_DISCOUNT_AMOUNT
            separateOrder.setBizNo(bizOrderNo.substring(0, bizOrderNo.indexOf(AmountTypeEnum.PLATFORM_DISCOUNT_AMOUNT.name()) - 1));
        }
        // 佣金
        else if (bizOrderNo.contains(AmountTypeEnum.BROKERAGE_AMOUNT.name())) {
            // RZD2025011601477_BROKERAGE_AMOUNT
            separateOrder.setBizNo(bizOrderNo.substring(0, bizOrderNo.indexOf(AmountTypeEnum.BROKERAGE_AMOUNT.name()) - 1));
        }
        // 平安信用支付买家会员间交易到卖家
        else if (bizOrderNo.contains(AmountTypeEnum.PINGAN_CREDIT_PAY_ACCT_TRANS.name())) {
            // 1879289835001233455_PINGAN_CREDIT_PAY_ACCT_TRANS
            separateOrder.setBizNo(bizOrderNo.substring(0, bizOrderNo.indexOf(AmountTypeEnum.PINGAN_CREDIT_PAY_ACCT_TRANS.name()) - 1));
        } else if (bizOrderNo.contains(AmountTypeEnum.XYD_CREDIT_FUND_PAY_ACCT_TRANS.name())) {
            separateOrder.setBizNo(bizOrderNo.substring(0, bizOrderNo.indexOf(AmountTypeEnum.XYD_CREDIT_FUND_PAY_ACCT_TRANS.name()) - 1));
        } else if (bizOrderNo.contains(AmountTypeEnum.XYD_CREDIT_PAYER_PAY_ACCT_TRANS.name())) {
            separateOrder.setBizNo(bizOrderNo.substring(0, bizOrderNo.indexOf(AmountTypeEnum.XYD_CREDIT_PAYER_PAY_ACCT_TRANS.name()) - 1));
        } else if (bizOrderNo.contains(AmountTypeEnum.XYD_CREDIT_REFUND_RETURN_BROKERAGE_AMOUNT.name())) {
            separateOrder.setBizNo(bizOrderNo.substring(0, bizOrderNo.indexOf(AmountTypeEnum.XYD_CREDIT_REFUND_RETURN_BROKERAGE_AMOUNT.name()) - 1));
        } else {
            separateOrder.setBizNo(bizOrderNo);
        }
    }

    /**
     * 转换
     */
    public SeparateOrder transferSeparateOrder() {
        SeparateOrder separateOrder = new SeparateOrder();
        separateOrder.setCustNoIn(inAccountId);
        separateOrder.setCustMerchant(outAccountId);
        separateOrder.setTradeAmount(amount);
        separateOrder.setBusinessOrderNo(businessOrderNo);
        fillSeparateOrderBizNo(separateOrder);
        separateOrder.setTradeCode(FbankTradeCodeEnum.FTC_1.getCode());
        separateOrder.setExtra("会员间交易");
        separateOrder.setStatus(SeparateOrder.STATUS_CREATE);
        separateOrder.setSettleNo(StringUtils.EMPTY);
        separateOrder.setCtime(DateUtil.now());
        separateOrder.setUtime(DateUtil.now());
        return separateOrder;
    }


    public SeparateOrder buildSeparateOrder() {
        SeparateOrder separateOrder = new SeparateOrder();
        separateOrder.setCustNoIn(inAccountId);
        separateOrder.setCustMerchant(outAccountId);
        separateOrder.setTradeAmount(amount);
        separateOrder.setBusinessOrderNo(businessOrderNo);
        separateOrder.setExtra(extra);
        separateOrder.setStatus(SeparateOrder.STATUS_CREATE);
        separateOrder.setSettleNo(StringUtils.EMPTY);
        separateOrder.setCtime(DateUtil.now());
        separateOrder.setUtime(DateUtil.now());
        return separateOrder;
    }
}
